import {
  Company
} from '../../model/company-model.js';

var company = new Company();

import {
  Order
} from '../../model/order-model.js';

var order = new Order();

import {
  Pay
} from '../../model/pay-model.js';

var pay = new Pay();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    capsuleTop: 0,
    capsuleHeight: 0,
    companyinfo: {},
    totaljobnum: 0,
    totalnotenum: 0,
    totaltopnum: 0,
    companyrole: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '企业中心',
    })

    // 获取菜单按钮信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
    this.setData({
      capsuleTop: menuButtonInfo.top,
      capsuleHeight: menuButtonInfo.height,
    })

    // 加载页面数据
    this.loadPageData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 页面显示时刷新数据
    this.loadPageData();
  },

  /**
   * 加载页面数据
   */
  loadPageData: function() {
    var ctoken = wx.getStorageSync('ctoken');
    var params = {
      ctoken: ctoken
    };

    // 显示加载提示
    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    company.checkLogin(() => {
      company.companycenter((data) => {
        // 隐藏加载提示
        wx.hideLoading();

        this.setData({
          companyinfo: data.companyinfo,
          totaljobnum: data.totaljobnum,
          totalnotenum: data.totalnotenum,
          totaltopnum: data.totaltopnum,
          companyrole: data.companyrole
        });
      }, params);
    }, params, (err) => {
      // 处理登录检查失败的情况
      wx.hideLoading();
      console.error('登录检查失败:', err);
    });
  },

  addcompanyjob: function () {
    var ctoken = wx.getStorageSync('ctoken');
    var params = {
      ctoken: ctoken
    };

    company.companycenter((data) => {
      if (data.totaljobnum < 1) {
        wx.showModal({
          title: '提示',
          content: '发布职位数量不足',
          showCancel: false
        })
        return
      } else {
        wx.navigateTo({
          url: "/pages/addcompanyjob/index"
        })
      }
    }, params);
  },

  toEditcompany: function () {
    wx.navigateTo({
      url: "/pages/editcompany/index"
    })
  },

  toCompanychat: function () {
    wx.navigateTo({
      url: "/pages/companychat/index"
    })
  },

  loginout: function () {
    const that = this;

    wx.showModal({
      title: '退出登录',
      content: '确定要退出当前账号吗？',
      confirmText: '退出',
      confirmColor: '#E64340',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '正在退出...',
            mask: true
          });

          // 清除相关存储数据
          wx.clearStorageSync('companyid');
          wx.clearStorageSync('ctoken');

          setTimeout(() => {
            wx.hideLoading();
            wx.showToast({
              title: '已退出',
              icon: 'success',
              duration: 1500,
              complete: () => {
                wx.reLaunch({
                  url: "/pages/companylogin/index",
                  success: () => {
                    console.log('退出后跳转成功');
                  },
                  fail: (err) => {
                    console.error('跳转失败:', err);
                    wx.showToast({
                      title: '跳转失败',
                      icon: 'error'
                    });
                  }
                });
              }
            });
          }, 800);
        } else if (res.cancel) {
          console.log('用户取消退出');
        }
      },
      fail: (err) => {
        console.error('弹窗显示失败:', err);
        wx.showToast({
          title: '操作失败',
          icon: 'error'
        });
      }
    });
  },

  toCompanyrole: function () {
    wx.navigateTo({
      url: "/pages/companyrole/index"
    })
  },

  toActive: function () {
    wx.navigateTo({
      url: "/pages/active/index"
    })
  },
  toMyComment: function () {
    wx.navigateTo({
      url: "/pages/companyregisteredit/index"
    })
  },
  toContact:function(){
    wx.navigateTo({
      url: '/pages/wechat/index?id='+1
    })
  },
  toMyjoblist: function (e) {
	  var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/pages/companyjob/index?type=" + type
    })
  },

  toCompanynote: function (e) {
    var type = e.currentTarget.dataset.type;
    console.log("type", type)
    if (type) {
      wx.navigateTo({
        url: "/pages/companynote/index?type=" + type
      })
    } else {
      wx.navigateTo({
        url: "/pages/companynote/index"
      })
    }
  },

  toSysmsg() {
    wx.redirectTo({
      url: '/pages/sysmsg/index'
    })
  },

  toMytasklist: function () {
    wx.navigateTo({
      url: "/pages/tasklist/index"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 下拉刷新时重新加载数据
    this.loadPageData();
    // 停止下拉刷新动画
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
